%Beta Smoothing estimation
function [betaSmoothing] = BetaSmoothing(rStock, rIndex, lambda)
n = length(rStock(:,1));

%Starting Point
varstock(1)=var(rStock,1);
varindex(1)=var(rIndex,1);
covariance=cov(rStock,rIndex);
covar(1)=covariance(1,2);
betaSmoothing(1)=covar(1)/varindex(1);
rStock=rStock(2:end,:);
rIndex=rIndex(2:end,:);
for i=1:n-1
    j=i+1;
    varstock(j)=varstock(i).*lambda +(1-lambda)*(rStock(i).^2);
    varindex(j)=varindex(i).*lambda +(1-lambda)*(rIndex(i).^2);
    covar(j)=covar(i).*lambda +(1-lambda)*(rStock(i)*rIndex(i));
    betaSmoothing(j)=covar(j)/ varindex(j);
end 

betaSmoothing = betaSmoothing';
end